package com.jbxie.spaceiotmqtt.mqttUtils;

import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.stereotype.Component;

import static com.jbxie.spaceiotmqtt.mqttUtils.MyMqttClient.*;

@Component
public class PublishThread implements Runnable {
    @Override
    public void run()
    {
        String[] obj = null;
        System.out.println("mqtt publish thread start");
        while (true) {
            obj = poll();
            if (obj != null) {
                String topic = obj[0];
                String content = obj[1];
                int qos = Integer.valueOf(obj[2]);
                System.out.println("mqtt从队列取出topic:" + topic + ",content:" + content + ",qos" + qos);
                try {
                    MyMqttClient.pubMessage(1, true, topic, content);
                    System.out.println("发送mqtt消息,topic: "+ topic +" ,content: " + content);
                } catch (Exception e) {
                    System.out.println("发消息给设备,topic:" + topic + ",content:" + content);
                    e.printStackTrace();
                }
            } else{
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
